<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.zhName"
        placeholder="角色名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.description"
        placeholder="角色描述"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-edit" @click="createRole">
        创建
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      style="width: 100%;"
    >
      <el-table-column label="序号" prop="id" align="center" width="60">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色编码" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.roleCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色名称" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.zhName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色描述">
        <template slot-scope="{row}">
          <span>{{ row.description }}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" width="160px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.creator }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作栏" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button round type="primary" size="mini" @click="handleUpdate(row)">
            分配用户
          </el-button>
          <el-button round type="primary" size="mini" @click="handleUpdate(row)">
            分配权限
          </el-button>
          <el-button round type="primary" size="mini" @click="handleUpdate(row)">
            编&nbsp&nbsp&nbsp&nbsp&nbsp辑
          </el-button>
          <el-button round size="mini" type="danger" @click="handleDelete(row,$index)">
            删&nbsp&nbsp&nbsp&nbsp&nbsp除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'
import { deleteRole, queryRoleByPage } from '../../../api/role' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      dialogFormVisible: true,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        description: '',
        zhName: '',
        startTime: '',
        endTime: ''
      },
      importanceOptions: [1, 2, 3],
      showReviewer: false,
      downloadLoading: false,
      pickerTime: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      queryRoleByPage(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    createRole() {
      this.$bus.emit('rolesDialog', { successFn: this.getList, isAddOrSave: true })
    },
    handleUpdate(row) {

    },
    handleDelete(row) {
      this.$bus.emit('conformDialog', {
        title: '提示', content: '此操作将永久删除该角色, 是否继续?', successFn: () => {
          deleteRole([row.id]).then(res => {
            this.$message({
              message: '删除成功！',
              type: 'success'
            })
            this.getList()
          })
        } })
    },
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    pickerTimeChange() {
      if (this.pickerTime) {
        this.listQuery.startTime = this.pickerTime[0].getTime()
        this.listQuery.endTime = this.pickerTime[1].getTime()
      } else {
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
      this.handleFilter()
    }
  }

}
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
  @import "~@/styles/variables.scss";
</style>
<style lang="scss">
  .el-table th.is-leaf, .el-table td{
    text-align: center;
  }
  .el-button.is-round{
    margin-top: 2px;
    width: 43%;
  }
</style>
